﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;

namespace DAO
{
    public class DoanhThuNam_DAO
    {
        public static DoanhThuNam_DTO CreateDTO(DoanhThuNam nam)
        {
            DoanhThuNam_DTO DTO = new DoanhThuNam_DTO();
            Mapper.CreateMap<DoanhThuNam, DoanhThuNam_DTO>();
            DTO = Mapper.Map<DoanhThuNam, DoanhThuNam_DTO>(nam);
            return DTO;
        }

        public List<DoanhThuNam_DTO> GetDoanhThuNamList()
        {
            List<DoanhThuNam_DTO> list = new List<DoanhThuNam_DTO>();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from nam in context.DoanhThuNams select nam;
                foreach (var cb in query)
                {
                    DoanhThuNam_DTO DTO = CreateDTO(cb);
                    list.Add(DTO);
                }
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
            return list;
        }

        public DoanhThuNam_DTO GetDoanhThuNamDetails(int Nam)
        {
            DoanhThuNam_DTO DTO = new DoanhThuNam_DTO();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from cb in context.DoanhThuNams
                            where cb.Nam == Nam
                            select cb;
                DoanhThuNam EF = query.First();
                DTO.MaDoanhNam = EF.MaDoanhThuNam;
                DTO.Nam = (int)EF.Nam;
                DTO.TongDoanhThuNam = (float)EF.TongDoanhThuNam;
            }
            catch (System.Exception)
            { }
            return DTO;
        }

        public static bool KiemTra(int Nam)
        {
            QLCBDataContext context = new QLCBDataContext();
            var query = from cb in context.DoanhThuNams
                        where cb.Nam == Nam
                        select cb;
            if (query.Count() == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static string MaDoanhThuNamMax()
        {
            QLCBDataContext context = new QLCBDataContext();
            string query = (from cb in context.DoanhThuNams select cb.MaDoanhThuNam).Max();
            return query;
        }

        public void InsertDoanhThuNam(DoanhThuNam_DTO DTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                DoanhThuNam nam = new DoanhThuNam();
                nam.MaDoanhThuNam = DTO.MaDoanhNam;
                nam.Nam = DTO.Nam;
                nam.TongDoanhThuNam = DTO.TongDoanhThuNam;
                context.DoanhThuNams.InsertOnSubmit(nam);
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void UpdateDoanhThuNam(DoanhThuNam_DTO DTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                DoanhThuNam nam = new DoanhThuNam();
                nam = context.DoanhThuNams.Single(cb => cb.MaDoanhThuNam == DTO.MaDoanhNam);
                nam.MaDoanhThuNam = DTO.MaDoanhNam;
                nam.Nam = DTO.Nam;
                nam.TongDoanhThuNam = DTO.TongDoanhThuNam;
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void DeleteDoanhThuNam(DoanhThuNam_DTO DTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                DoanhThuNam nam = context.DoanhThuNams.Single(cb => cb.MaDoanhThuNam == DTO.MaDoanhNam);
                context.DoanhThuNams.DeleteOnSubmit(nam);
                context.SubmitChanges();
            }
            catch
            { }
        }
    }
}
